package com.alisls.shequtao.service.goods.config;

import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * @author Ke Wang
 */
@Configuration
@RefreshScope
@Slf4j
public class SwaggerConfig {

    @Value("${swagger.prop.title}")
    private String title;

    @Value("${swagger.prop.version}")
    private String version;

    @Value("${swagger.prop.description}")
    private String description;

    @Value("${swagger.prop.base-package}")
    private String basePackage;

    @Value("${swagger.enabled}")
    private Boolean swaggerEnabled;

    @Value("${swagger.contact.name}")
    private String name;

    @Value("${swagger.contact.email}")
    private String email;

    @Value("${swagger.contact.url}")
    private String url;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("商品微服务")
                .pathMapping("/")
                .enable(swaggerEnabled)
                .apiInfo(apiInfo())
                .select()
                // 为当前包路径
                //.apis(RequestHandlerSelectors.basePackage(basePackage))
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.any())
                //.paths(PathSelectors.regex("/user/.*"))
                .build();
    }

    private ApiInfo apiInfo() {
        Contact contact = new Contact(name, url, email);
        // 构建 api文档的详细信息函数,注意这里的注解引用的是哪个
        return new ApiInfoBuilder()
                // 页面标题
                .title(title)
                // 版本号
                .version(version)
                // 描述
                .description(description)
                .termsOfServiceUrl("http://www.alisls.com")
                .contact(contact)
                .build();
    }

}
